<template>
  <div>
    <!-- 工单表单 -->
    <el-drawer
      ref="drawer"
      :title="title"
      direction="rtl"
      :visible.sync="open"
      size="80%"
      append-to-body
      :wrapperClosable="false"
    >
      <el-card
        class="box-card"
        style="width: 96%; margin-left: 20px; min-height: 90vh"
      >
        <el-row>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-col :span="12">
              <el-form-item label="工单类型" prop="taskType">
                <el-select
                  v-model="form.taskType"
                  filterable
                  :disabled="true"
                  placeholder="请选择工单类型"
                  style="width: 100%"
                >
                  <el-option
                    v-for="dict in dict.type.task_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客户名称" prop="customerId">
                <el-select
                  v-model="form.customerId"
                  filterable
                  :disabled="true"
                  placeholder="请选择客户名称"
                  style="width: 100%"
                  @change="getCrmOrderAllNoPage(form.customerId)"
                >
                  <el-option
                    v-for="customer in customerList"
                    :key="customer.customerId"
                    :label="customer.customerName"
                    :value="customer.customerId"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="地区" prop="region">
                <el-cascader
                  :disabled="true"
                  v-model="form.region"
                  :options="cityTree"
                  :props="cascaderProps"
                  style="width: 100%"
                ></el-cascader>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="商机名称" prop="orderId">
                <el-select
                  v-model="form.orderId"
                  filterable
                  :disabled="true"
                  placeholder="请选择商机名称"
                  style="width: 100%"
                >
                  <el-option
                    v-for="order in orderListAll"
                    :key="order.orderId"
                    :label="order.orderName"
                    :value="order.orderId"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开始时间" prop="startTime">
                <el-date-picker
                  style="width: 100%"
                  clearable
                  v-model="form.startTime"
                  type="date"
                  :disabled="true"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择开始时间"
                />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="结束时间" prop="endTime">
                <el-date-picker
                  style="width: 100%"
                  clearable
                  v-model="form.endTime"
                  type="date"
                  :disabled="true"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择结束时间"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="紧急程度" prop="taskLevel">
                <el-select
                  :disabled="true"
                  v-model="form.taskLevel"
                  filterable
                  placeholder="请选择紧急程度"
                  style="width: 100%"
                >
                  <el-option
                    v-for="dict in dict.type.task_level"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="实施类型" prop="taskMode">
                <el-select
                  :disabled="true"
                  v-model="form.taskMode"
                  filterable
                  placeholder="请选择实施类型"
                  style="width: 100%"
                >
                  <el-option
                    v-for="dict in dict.type.task_mode"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="负责人" prop="userId">
                <el-select
                  :disabled="true"
                  v-model="form.userId"
                  filterable
                  placeholder="请选择负责人"
                  style="width: 100%"
                >
                  <el-option
                    v-for="order in userList"
                    :key="order.userId"
                    :label="order.userName"
                    :value="order.userId"
                  />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="应收金额" prop="receivablePayable">
                <el-input
                  :disabled="true"
                  v-model.number="form.receivablePayable"
                  placeholder="请输入应收金额"
                  type="number"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="已收金额" prop="receivablePaid">
                <el-input
                  :disabled="true"
                  type="number"
                  v-model="form.receivablePaid"
                  placeholder="请输入已收金额款"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="实施价格" prop="taskPrice">
                <el-input
                  :disabled="true"
                  type="number"
                  v-model="form.taskPrice"
                  placeholder="请输入实施价格"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="实际天数" prop="actualDay">
                <el-input
                  :disabled="true"
                  v-model.number="form.actualDay"
                  placeholder="请输入实际天数"
                  type="number"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="工作日志" prop="remark">
                <el-input
                  :disabled="true"
                  v-model="form.remark"
                  type="textarea"
                  placeholder="请输入工作日志"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="工单说明" prop="illustrate">
                <el-input
                  :disabled="true"
                  v-model="form.illustrate"
                  type="textarea"
                  placeholder="请输入工单说明"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-divider>审批通道</el-divider>
              <el-form-item label="电子签名" prop="signature">
                <div class="box">
                  <VueSignaturePad
                    width="100%"
                    height="250px"
                    ref="signaturePad"
                  />
                  <el-button @click="undo" type="text" class="box-button"
                    >清空签名</el-button
                  >
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="审批意见" prop="message">
                <el-input
                  v-model="form.message"
                  type="textarea"
                  placeholder="请输入审批意见"
                  :autosize="{ minRows: 4, maxRows: 4 }"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="!disabled">
              <el-form-item>
                <el-button type="primary" @click="handleBtn('PASS')"
                  >通过</el-button
                >
                <el-button @click="handleBtn('REJECT')">驳回</el-button>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </el-card>
    </el-drawer>
  </div>
</template>

<script>
import { getTask, audit } from "@/api/crm/task";
import { getCityTreeAllByParentId } from "@/api/system/city";
import { getCrmOrderAllNoPage } from "@/api/crm/order";
import { getCrmCustomerAllNoPage } from "@/api/crm/customer";
import { getUserListByStatus } from "@/api/system/user";
export default {
  name: "Taskove",
  dicts: ["flow_status", "task_type", "task_level", "task_mode", "task_status"],
  props: {
    /* 业务id */
    value: {
      type: String,
      default: "",
    },

    /* 实例id */
    taskId: {
      type: String,
      default: "",
    },

    /* 是否可以标编辑 */
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      cascaderProps: {
        value: "label", // 选项的ID字段
        label: "label", // 选项的显示字段
        children: "children", // 子节点字段
        // checkStrictly: true,
      },
      cityTree: [],
      orderList: [],
      orderListAll: [],
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 指定跳转节点
      nodeCode: "",
      customerList: [],
      orderList: [],
      rules: {
        // signature: [
        //   {
        //     required: true,
        //     validator: this.validateDetails,
        //     trigger: "change",
        //   },
        // ],
      },
    };
  },
  created() {
    this.reset();
    this.getTask();
    this.getCrmOrderAllNoPage();
    this.getCityTree();
    this.getCrmCustomerAllNoPage();
    this.getUserListByStatus();
  },
  mounted(){
    setTimeout(() => {
      let data = window.localStorage.getItem("签名_" + this.$store.getters.userId)
      if(data){
        this.$refs.signaturePad.fromDataURL(data);
      }
    }, 500)
  },
  methods: {
    async getCityTree() {
      await getCityTreeAllByParentId(1).then((response) => {
        this.cityTree = response.data;
        this.cityTree = this.moveJiangxiToFirst(this.cityTree)
      });
    },
    async getCrmOrderAllNoPage() {
      await getCrmOrderAllNoPage({}).then((res) => {
        this.orderListAll = res.data;
      });
    },
    async getCrmCustomerAllNoPage() {
      await getCrmCustomerAllNoPage({}).then((res) => {
        this.customerList = res.data;
      });
    },
    async getUserListByStatus() {
      await getUserListByStatus({ status: "0" }).then((res) => {
        this.userList = res.data;
      });
    },

    validateDetails(rule, value, callback) {
      const { isEmpty, data } = this.$refs.signaturePad.saveSignature();
      // alert(isEmpty)
      if (isEmpty) {
        callback(new Error("电子签名必须填写"));
      } else {
        callback();
      }
    },
    // 撤销操作
    undo() {
      this.$refs.signaturePad.clearSignature();
    },
    async getTask() {
      await getTask(this.value).then((response) => {
        if (!response.data) {
          this.$modal.alertWarning("代办任务不存在");
        }
        console.log(response.data);
        this.form = response.data;
        this.form.region = this.form.region
          ? this.form.region.split(",")
          : null;
        this.title = this.form.createBy + " 工单审批";
        this.open = true;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        message: null,
        id: null,
        type: null,
        reason: null,
        startTime: null,
        endTime: null,
        day: null,
        instanceId: null,
        nodeCode: null,
        nodeName: null,
        flowStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 审核通过按钮 */
    handleBtn(skipType) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.region = this.form.region?.join(",");
          this.form.taskId = this.taskId;
          this.form.skipType = skipType;
          const { isEmpty, data } = this.$refs.signaturePad.saveSignature();
          this.form.signature = data;
          audit(this.form).then((response) => {
            this.$modal.msgSuccess("办理成功");
            window.localStorage.setItem("签名_" + this.$store.getters.userId, this.form.signature)
            this.open = false;
            this.$emit("refresh");
          });
        }
      });
    },
  },
};
</script>

<style scoped>
::v-deep .el-drawer__header {
  margin-bottom: 20px;
}
::v-deep .el-input.is-disabled .el-input__inner {
  background-color: white;
  border-color: #dfe4ed;
  color: black;
  cursor: not-allowed;
}

::v-deep .el-textarea.is-disabled .el-textarea__inner {
  background-color: white;
  border-color: #dfe4ed;
  color: black;
  cursor: not-allowed;
}

::v-deep .details .el-table--medium .el-table__cell {
  padding: 0;
}

::v-deep .details .el-input--medium {
  margin: 10px 10px 10px -10px;
}

.box {
  width: 100%;
  height: 250px;
  border: 1px solid #dcdfe6;
  position: relative;
}

.box-button {
  position: absolute;
  top: 215px;
  left: 93%;
}
</style>